using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BackgroundPanel : MonoBehaviour
{
    public RectTransform m_FollowImage;

    public Color m_BlockPointColor;
    public bool m_IsBackground = false;
    private void Awake()
    {
        var image = GetComponent<Image>();
        //image.sprite = Sprite.Create(Texture2D.CreateExternalTexture((int)Ship.Level.D, (int)Ship.Level.D, TextureFormat.ARGB32, false, false , null));

    }
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if(m_FollowImage)
        {
            var rectTransform = GetComponent<RectTransform>();
            rectTransform.position = m_FollowImage.position;
            rectTransform.sizeDelta = m_FollowImage.sizeDelta;
        }
    }

    public void OnLevelCanged(int value)
    {

        Ship.Level shipLevel = Ship.Level.D;
        switch (value)
        {
            case 0:
                shipLevel = Ship.Level.D;
                break;
            case 1:
                shipLevel = Ship.Level.C;
                break;
            case 2:
                shipLevel = Ship.Level.B;
                break;
            case 3:
                shipLevel = Ship.Level.A;
                break;
            case 4:
                shipLevel = Ship.Level.AP;
                break;
        }
        Resize((int)shipLevel);

    }

    void Resize(int size)
    {
        if (m_IsBackground == false)
            return;
        var image = GetComponent<Image>();
        var texture = image.sprite.texture;
        texture.Reinitialize(size, size);
        for(int i=0; i<size; ++i)
        {
            for(int j=0; j<size; ++j)
            {
                bool express1 = false;
                if ((i % 2) == 0)
                    express1 = ((j % 2) == 0);
                else
                    express1 = ((j % 2) != 0);

                if (express1)
                    texture.SetPixel(i, j, m_BlockPointColor);
                else
                    texture.SetPixel(i, j, Color.white);
            }
        }
        texture.Apply();
    }
}
